<template>
  <div class="login-container">
    <el-form class="myform" :model="form" :rules="rules" ref="myform">
      <h3 class="logo"></h3>
      <el-form-item prop="mobile">
        <el-input
          prefix-icon="el-icon-user-solid"
          v-model="form.mobile"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          prefix-icon="el-icon-lock"
          v-model="form.password"
          show-password
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" class="btn" @click="login"
          >登录</el-button
        >
      </el-form-item>
      <div class="mark">还没有账号？立即登录</div>
    </el-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data () {
    return {
      form: { mobile: '13800000002', password: '123456' },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: function (rule, value, callback) {
              var reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            }
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 16,
            message: '密码必须在6~16位',
            trigger: 'blur'
          }
        ]
      },
      loading: false
    }
  },
  methods: {
    ...mapActions({ userLogin: 'user/saveToken' }),
    login () {
      this.$refs.myform.validate(async valid => {
        // console.log(valid
        if (valid) {
          this.loading = true
          try {
            await this.userLogin(this.form)
            const redirect=this.$route.query._redirect
            if (redirect) {
              this.$router.push(redirect)
            } else {
              
              this.$router.push('/') 
            }
            this.$message.success('登录成功')
          } catch (error) {
            console.log(error.message)
          } finally {
            this.loading = false
          }
        } else {
          console.log('验证失败')
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-container {
  width: 100%;
  height: 100%;
  background-image: url('~@/assets/common/login.jpg');
  background-position: center;
  .logo {
    background: url('~@/assets/common/login-logo.png') no-repeat;
    width: 436px;
    height: 44px;
    margin-bottom: 40px;
  }
  .myform {
    width: 520px;
    height: 400px;
    padding: 0 35px 15px;
    position: fixed;
    top: 45%;
    left: 50%;
    transform: translate(-50%, -50%);

    .btn {
      width: 100%;
      height: 60px;
      background-color: #407ffe;
      border: none;
      font-size: 20px;
      color: #fff;
    }

    .el-input__inner {
      height: 50px;
      background-color: #d5e5ff;
      color: #407ffe;
      outline: none;
      border: none;
    }

    .mark {
      font-size: 16px;
      color: #fff;
      text-align: center;
    }

    .el-form-item__error {
      color: #fff;
    }
  }
}
</style>
